<?php


namespace app\common\server;

class FileServer
{
	public static function image($cid, $admin_info, $save_dir = "uploads/images/store_image")
	{
		try {
			if (!file_exists($save_dir)) {
				mkdir($save_dir, 509, true);
			}
			$file = request()->file("file");
			$info = $file->validate(["ext" => "jpg,png,gif,jpeg,bmp"])->move($save_dir);
			if (empty($info)) {
				throw new \think\Exception($file->getError());
			} else {
				$data = ["sid" => $admin_info["sid"], "bid" => empty($admin_info["bid"]) ? 0 : $admin_info["bid"], "name" => $file->getInfo("name"), "type" => \app\common\model\File::type_image, "uri" => $save_dir . "/" . str_replace("\\", "/", $info->getSaveName()), "create_time" => time(), "cid" => $cid];
				\think\Db::name("store_file")->insert($data);
			}
			return ["code" => 1, "msg" => "上传成功"];
		} catch (\think\Exception $e) {
			$messge = $e->getMessage();
			return ["code" => -1, "msg" => "上传文件失败:" . $messge];
		}
	}
	public static function dataSuccess($msg = "", $data = [], $code = 1, $show = 1)
	{
		return ["msg" => $msg, "data" => $data, "code" => $code, "show" => $show];
	}
	public static function dataError($msg = "", $data = [], $code = 0, $show = 1)
	{
		return ["msg" => $msg, "data" => $data, "code" => $code, "show" => $show];
	}
	public static function lists($pag_no, $page_size, $get, $type, $admin_info)
	{
		$where[] = ["sid", "=", $admin_info["sid"]];
		if (!empty($admin_info["bid"])) {
			$where[] = ["bid", "=", $admin_info["bid"]];
		} else {
			$where[] = ["bid", "=", 0];
		}
		if ($type) {
			$where["type"] = $type;
		}
		if (!empty($get["cid"]) && $get["cid"] != 0) {
			$where[] = ["cid", "=", $get["cid"]];
		}
		if (!empty($get["name"])) {
			$where[] = ["name", "like", "%{$get["name"]}%"];
		}
		$siteurl = getsiteurl() . "/";
		$siteurl = ($_SERVER["SERVER_PORT"] == "443" ? "https://" : "http://") . $_SERVER["SERVER_NAME"] . "/";
		$count = \think\Db::name("store_file")->where($where)->count();
		$pages = ceil($count / $page_size);
		$lists = \think\Db::name("store_file")->field(["name", "uri as path", "CONCAT('{$siteurl}',uri) as uri", "id", "cid"])->where($where)->page($pag_no, $page_size)->order(["id" => "desc"])->select();
		$data = [];
		foreach ($lists as $file_item) {
			$file_item["address"] = $siteurl . "index/system/get_custom_image/name/" . base64_encode($file_item["path"]);
			if ($file_item["cid"] == 0) {
				$file_item["category_name"] = "默认分类";
			} else {
				$file_item["category_name"] = \think\Db::name("store_file_classify")->where(["id" => $file_item["cid"]])->value("name");
			}
			$data[] = $file_item;
		}
		return ["code" => 1, "msg" => "获取成功", "count" => $count, "pages" => $pages, "data" => $data];
	}
	public static function del($ids, $admin_info)
	{
		$where["sid"] = $admin_info["sid"];
		if (!empty($admin_info["bid"])) {
			$where["bid"] = $admin_info["bid"];
		} else {
			$where["bid"] = 0;
		}
		$uri = \think\Db::name("store_file")->where("id", "in", $ids)->where($where)->column("uri");
		$res = \think\Db::name("store_file")->where("id", "in", $ids)->where($where)->delete();
		foreach ($uri as $path) {
			unlink($path);
		}
		return $res;
	}
	public static function classify_info($id, $admin_info)
	{
		$where["sid"] = $admin_info["sid"];
		if (!empty($admin_info["bid"])) {
			$where["bid"] = $admin_info["bid"];
		} else {
			$where["bid"] = 0;
		}
		return \think\Db::name("store_file_classify")->where(["id" => $id])->where($where)->field(["id", "name", "pid", "level", "sort", "create_time", "update_time"])->find();
	}
	public static function add_classify($post, $admin_info)
	{
		$where["sid"] = $admin_info["sid"];
		if (!empty($admin_info["bid"])) {
			$where["bid"] = $admin_info["bid"];
		} else {
			$where["bid"] = 0;
		}
		$level = 0;
		if ($post["pid"]) {
			$level = \think\Db::name("store_file_classify")->where(["id" => $post["pid"]])->where($where)->value("level");
		}
		$data = ["sid" => $admin_info["sid"], "bid" => empty($admin_info["bid"]) ? 0 : $admin_info["bid"], "name" => $post["name"], "pid" => $post["pid"], "sort" => $post["sort"], "level" => $level + 1, "create_time" => time()];
		return \think\Db::name("store_file_classify")->insert($data);
	}
	public static function edit_classify($post, $admin_info)
	{
		$where["sid"] = $admin_info["sid"];
		if (!empty($admin_info["bid"])) {
			$where["bid"] = $admin_info["bid"];
		} else {
			$where["bid"] = 0;
		}
		$level = 0;
		$data = ["pid" => $post["pid"], "name" => $post["name"], "sort" => $post["sort"], "update_time" => time()];
		if ($post["pid"]) {
			$level = \think\Db::name("store_file_classify")->where(["id" => $post["pid"]])->where($where)->value("level");
		}
		$data["level"] = $level + 1;
		return \think\Db::name("store_file_classify")->where("id", $post["id"])->update($data);
	}
	public static function del_classify($id, $admin_info)
	{
		$where["sid"] = $admin_info["sid"];
		if (!empty($admin_info["bid"])) {
			$where["bid"] = $admin_info["bid"];
		} else {
			$where["bid"] = 0;
		}
		\think\Db::name("store_file_classify")->where(["id" => $id])->where($where)->delete();
		$image_ids = \think\Db::name("store_file")->where(["cid" => $id])->column("id");
		self::del($image_ids, $admin_info);
		return true;
	}
	public static function cateTree()
	{
		$lists = \think\Db::name("store_file_classify")->where(["store" => get_admin_cache_store_id()])->order("sort")->select();
		$tree = self::cateListToTree($lists, 0);
		$all = ["id" => 0, "field" => "all", "title" => "全部", "children" => []];
		array_unshift($tree, $all);
		return $tree;
	}
	public static function cateListToTree($lists, $pid = 0)
	{
		$tree = [];
		foreach ($lists as $k => $v) {
			if ($v["pid"] == $pid) {
				$temp["id"] = $v["id"];
				$temp["field"] = "id";
				$temp["title"] = $v["name"];
				$temp["children"] = self::cateListToTree($lists, $v["id"]);
				$temp["spread"] = true;
				$tree[] = $temp;
			}
		}
		return $tree;
	}
	public static function categoryToSelect()
	{
		$lists = \think\Db::name("store_file_classify")->where(["store" => get_admin_cache_store_id()])->order("sort")->select();
		$tree = [];
		foreach ($lists as $val) {
			if ($val["pid"] != 0) {
				continue;
			}
			$tree[$val["id"]] = "|----" . $val["name"];
			foreach ($lists as $val2) {
				if ($val2["pid"] == $val["id"]) {
					$tree[$val2["id"]] = "|--------" . $val2["name"];
				}
			}
		}
		return $tree;
	}
}